Date.prototype.toLocaleString = function() {
    return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒";
};

function addNote(){
    const title = $('#title').val()
    const content = $('#content').val()
    if(!title||!content){
        alert('标题和内容都不能为空')
        return;
    }
    $.post('http://localhost:8080/note',{
        title:title,
        content:content,
        createTime:Date.now()
    },function(res){
        console.log(res)
        getList()
    })
}
function getList(){
    $.get('http://localhost:8080/getList.do',function(res){
        console.log(res.data)
        let addListHtml = '<tr>\n' +
            '                <th>序号</th>\n' +
            '                <th>标题</th>\n' +
            '                <th>内容</th>\n' +
            '                <th>时间</th>\n' +
            '                <th>操作</th>\n' +
            '            </tr>';
        $.each(res.data,function(k,e){
            addListHtml+= '<tr>\n' +
                '                <td>'+(k+1)+'</td>\n' +
                '                <td>'+e.title+'</td>\n' +
                '                <td>'+e.content+'</td>\n' +
                '                <td>'+new Date(e.createTime).toLocaleString()+'</td>\n' +
                '                <td>' +
                '                       <a href="javascript:void(0)" onclick="updata(\''+e._id+'\',\''+e.title+'\',\''+e.content+'\')">修改</a> ' +
                '                       <a href="javascript:void(0)" onclick="delData(\''+e._id+'\')">删除</a></td>\n' +
                '            </tr>'


        })
        if(res.data.length){
            $('#list').empty()
            $('#list').append(addListHtml)
        }
    })
}

function updata(id,title,content){
    console.log(id)
    let _data = {}
    _data.title = prompt('请输入标题',title)
    _data.content = prompt('请输入内容',content)

    if((title!=_data.title)||(content!=_data.content)){
        $.ajax({
            url: 'http://localhost:8080/put/'+id,
            type: 'PUT',
            data:_data,
            success: function( response ) {
                console.log(response)
                getList()
            }
        });
    }
}


function delData(id){
    $.ajax({
        url: 'http://localhost:8080/delete/'+id,
        type: 'DELETE',
        success: function(result) {
            // Do something with the result
            console.log(result)
            getList()
        }
    });


}

getList()